A h/t to Sharon Machlis for turning people on to the Mapview package, and her excellent walkthroughs of it.
For the data tonight we’ll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let’s quickly review how we use it.
# uncomment to run, then recomment it out so you don't run it every timecensus_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite=TRUE)
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
#chose variables we wantmyvars <-c(totalpop ="B01003_001",medincome ="B19013_001",medage ="B01002_001")
#pull for VA countiesva_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),state ="VA",output ="wide",geometry =TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Simple feature collection with 133 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE medincomeM
1 51105 Lee County, Virginia 22482 NA 37574 4430
2 51163 Rockbridge County, Virginia 22663 NA 57828 3580
3 51001 Accomack County, Virginia 33388 NA 50601 5803
4 51099 King George County, Virginia 26597 NA 101599 7080
5 51185 Tazewell County, Virginia 40615 NA 42937 3086
6 51029 Buckingham County, Virginia 16832 NA 49841 5835
7 51047 Culpeper County, Virginia 52021 NA 85274 5034
8 51179 Stafford County, Virginia 154204 NA 119818 3302
9 51085 Hanover County, Virginia 109232 NA 96911 2936
10 51009 Amherst County, Virginia 31381 NA 60876 3561
medageE medageM geometry
1 45.6 0.7 MULTIPOLYGON (((-83.67461 3...
2 49.2 0.6 MULTIPOLYGON (((-79.68695 3...
3 47.1 0.6 MULTIPOLYGON (((-75.74241 3...
4 38.3 0.4 MULTIPOLYGON (((-77.34744 3...
5 45.1 0.3 MULTIPOLYGON (((-81.90089 3...
6 43.0 0.8 MULTIPOLYGON (((-78.83284 3...
7 39.0 0.6 MULTIPOLYGON (((-78.22926 3...
8 35.9 0.2 MULTIPOLYGON (((-77.63422 3...
9 42.1 0.4 MULTIPOLYGON (((-77.79681 3...
10 44.9 0.5 MULTIPOLYGON (((-79.43695 3...
#all counties in the USall_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),output ="wide",geometry =TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
all_counties_withgeo
Simple feature collection with 3221 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1489 ymin: 17.88328 xmax: 179.7785 ymax: 71.36516
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE totalpopM medincomeE
1 20161 Riley County, Kansas 72602 NA 53296
2 19159 Ringgold County, Iowa 4739 NA 57700
3 30009 Carbon County, Montana 10488 NA 63178
4 16007 Bear Lake County, Idaho 6327 NA 60337
5 55011 Buffalo County, Wisconsin 13314 NA 61167
6 31185 York County, Nebraska 14164 NA 66337
7 08037 Eagle County, Colorado 55693 NA 91338
8 42129 Westmoreland County, Pennsylvania 355107 NA 64708
9 40079 Le Flore County, Oklahoma 48436 NA 43049
10 48053 Burnet County, Texas 48424 NA 65363
medincomeM medageE medageM geometry
1 2489 25.5 0.1 MULTIPOLYGON (((-96.96095 3...
2 5058 44.3 1.0 MULTIPOLYGON (((-94.47167 4...
3 4261 50.7 0.9 MULTIPOLYGON (((-109.7987 4...
4 7039 38.9 1.1 MULTIPOLYGON (((-111.6345 4...
5 2352 46.5 0.5 MULTIPOLYGON (((-92.08384 4...
6 4128 39.5 1.2 MULTIPOLYGON (((-97.82629 4...
7 4058 37.8 0.8 MULTIPOLYGON (((-107.1137 3...
8 1350 47.1 0.2 MULTIPOLYGON (((-79.90487 4...
9 1869 38.8 0.3 MULTIPOLYGON (((-95.05996 3...
10 4694 44.7 0.3 MULTIPOLYGON (((-98.45924 3...
#remove MOE columns - they all end with "M"va_counties_withgeo <- va_counties_withgeo %>%select(-ends_with("M"))va_counties_withgeo
Simple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAME totalpopE medincomeE medageE
1 51105 Lee County, Virginia 22482 37574 45.6
2 51163 Rockbridge County, Virginia 22663 57828 49.2
3 51001 Accomack County, Virginia 33388 50601 47.1
4 51099 King George County, Virginia 26597 101599 38.3
5 51185 Tazewell County, Virginia 40615 42937 45.1
6 51029 Buckingham County, Virginia 16832 49841 43.0
7 51047 Culpeper County, Virginia 52021 85274 39.0
8 51179 Stafford County, Virginia 154204 119818 35.9
9 51085 Hanover County, Virginia 109232 96911 42.1
10 51009 Amherst County, Virginia 31381 60876 44.9
geometry
1 MULTIPOLYGON (((-83.67461 3...
2 MULTIPOLYGON (((-79.68695 3...
3 MULTIPOLYGON (((-75.74241 3...
4 MULTIPOLYGON (((-77.34744 3...
5 MULTIPOLYGON (((-81.90089 3...
6 MULTIPOLYGON (((-78.83284 3...
7 MULTIPOLYGON (((-78.22926 3...
8 MULTIPOLYGON (((-77.63422 3...
9 MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...
#remove that trailing "E"colnames(va_counties_withgeo) <-sub("E$", "", colnames(va_counties_withgeo)) # $ means end of string onlyva_counties_withgeo
Simple feature collection with 133 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -83.67539 ymin: 36.54074 xmax: -75.24247 ymax: 39.46601
Geodetic CRS: NAD83
First 10 features:
GEOID NAM totalpop medincome medage
1 51105 Lee County, Virginia 22482 37574 45.6
2 51163 Rockbridge County, Virginia 22663 57828 49.2
3 51001 Accomack County, Virginia 33388 50601 47.1
4 51099 King George County, Virginia 26597 101599 38.3
5 51185 Tazewell County, Virginia 40615 42937 45.1
6 51029 Buckingham County, Virginia 16832 49841 43.0
7 51047 Culpeper County, Virginia 52021 85274 39.0
8 51179 Stafford County, Virginia 154204 119818 35.9
9 51085 Hanover County, Virginia 109232 96911 42.1
10 51009 Amherst County, Virginia 31381 60876 44.9
geometry
1 MULTIPOLYGON (((-83.67461 3...
2 MULTIPOLYGON (((-79.68695 3...
3 MULTIPOLYGON (((-75.74241 3...
4 MULTIPOLYGON (((-77.34744 3...
5 MULTIPOLYGON (((-81.90089 3...
6 MULTIPOLYGON (((-78.83284 3...
7 MULTIPOLYGON (((-78.22926 3...
8 MULTIPOLYGON (((-77.63422 3...
9 MULTIPOLYGON (((-77.79681 3...
10 MULTIPOLYGON (((-79.43695 3...
Warning: Found less unique colors (9) than unique zcol values (133)!
Interpolating color vector to match number of zcol values.
Customize popups
mypopup <- glue::glue("<strong>{va_counties_withgeo$NAM}</strong><br /> Total Population: {va_counties_withgeo$totalpop}<br /> Median Income: {va_counties_withgeo$medincome}") %>%lapply(htmltools::HTML)# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>%# lapply(htmltools::HTML)
head(mypopup)
[[1]]
<strong>Lee County, Virginia</strong><br />
Total Population: 22482<br />
Median Income: 37574
[[2]]
<strong>Rockbridge County, Virginia</strong><br />
Total Population: 22663<br />
Median Income: 57828
[[3]]
<strong>Accomack County, Virginia</strong><br />
Total Population: 33388<br />
Median Income: 50601
[[4]]
<strong>King George County, Virginia</strong><br />
Total Population: 26597<br />
Median Income: 101599
[[5]]
<strong>Tazewell County, Virginia</strong><br />
Total Population: 40615<br />
Median Income: 42937
[[6]]
<strong>Buckingham County, Virginia</strong><br />
Total Population: 16832<br />
Median Income: 49841